<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
</head>

<body>
</body>

<script>
    // 减法 把所有数都pow成整数，再减，再 / 成原来的小数位，再用tofixed原来的小数位进行精度取值
    function optimizeSub(arr) {
        if (!arr || arr.length === 0) {
            return 0;
        }
        else if (arr.length === 1) {
            return arr[0]
        }
        let decimalLengthArr = [], precision, maxDecimalLength
        arr.forEach(item => {
            let dividingDecimalsIntegers = item.toString().split('.');
            decimalLengthArr = [...decimalLengthArr, dividingDecimalsIntegers?.length > 1 ? dividingDecimalsIntegers[1].length : 0]
        });
        maxDecimalLength = Math.max(...decimalLengthArr);
        precision = Math.pow(10, maxDecimalLength)
        return (arr.reduce((pre, next) => (pre * precision - next * precision) / precision)).toFixed(maxDecimalLength)
    }

    // 加法  同减法，只是redeuce不同
    function optimizeAdd(arr) {
        if (!arr || arr.length === 0) {
            return 0;
        }
        else if (arr.length === 1) {
            return arr[0]
        }
        let decimalLengthArr = [], precision, maxDecimalLength
        arr.forEach(item => {
            let dividingDecimalsIntegers = item.toString().split('.');
            decimalLengthArr = [...decimalLengthArr, dividingDecimalsIntegers?.length > 1 ? dividingDecimalsIntegers[1].length : 0]
        });
        maxDecimalLength = Math.max(...decimalLengthArr);
        precision = Math.pow(10, maxDecimalLength)
        return (arr.reduce((pre, next) => (pre * precision + next * precision) / precision)).toFixed(maxDecimalLength)
    }

    // 乘法，不再是单独数组里数字的最大长度小数位，而是所有小数位相加
    function optimizeMul(arr) {
        var decimalLength = 0;
        arr.forEach(item => {
            let dividingDecimalsIntegers = item.toString().split('.');
            decimalLength += dividingDecimalsIntegers?.length > 1 ? dividingDecimalsIntegers[1].length : 0;
        })
        return (arr.reduce((pre, next) => {
            return Number(String(pre).replace('.', '')) * Number(String(next).replace('.', ''));
        })) / Math.pow(10, decimalLength)
    }

    // 除法 相当于把除数和被除数约分，只留下整数除数和小数被除数，再用pow进行赋小数
    function optimizeDiv(arr) {
        var decimalLengthArr = [];
        arr = arr.map((item, index) => {
            let dividingDecimalsIntegers = item.toString().split('.');
            decimalLengthArr = [...decimalLengthArr, dividingDecimalsIntegers?.length > 1 ? dividingDecimalsIntegers[1].length : 0]
            return Number(String(item).replace('.', ''))
        })
        return arr.reduce((pre, next, nextIndex, sourceArray) => pre / next * Math.pow(10, decimalLengthArr[nextIndex] - decimalLengthArr[nextIndex - 1]))
    }


    function accAdd(arg1, arg2) {
        let decimalPlaces1 = arg1.toString().split('.')
        let decimalPlaces2 = arg2.toString().split('.')
        maxDecimalLength = Math.max(decimalPlaces1.length > 1 ? decimalPlaces1[1].length : 0, decimalPlaces2.length > 1 ? decimalPlaces2[1].length : 0)
        powDecimalLength = Math.pow(10, maxDecimalLength)
        return ((arg1 * powDecimalLength + arg2 * powDecimalLength) / powDecimalLength).toFixed(maxDecimalLength);
    }

    function accSub(arg1, arg2) {
        let decimalPlaces1 = arg1.toString().split('.')
        let decimalPlaces2 = arg2.toString().split('.')
        maxDecimalLength = Math.max(decimalPlaces1.length > 1 ? decimalPlaces1[1].length : 0, decimalPlaces2.length > 1 ? decimalPlaces2[1].length : 0)
        powDecimalLength = Math.pow(10, maxDecimalLength)
        return ((arg1 * powDecimalLength - arg2 * powDecimalLength) / powDecimalLength).toFixed(maxDecimalLength);
    }

    function accMul(arg1, arg2) {
        let decimalPlaces1 = arg1.toString().split('.')
        let decimalPlaces2 = arg2.toString().split('.')
        return (Number(String(arg1).replace('.', '')) * Number(String(arg2).replace('.', ''))) / Math.pow(10, (decimalPlaces1.length > 1 ? decimalPlaces1[1].length : 0) + (decimalPlaces2.length > 1 ? decimalPlaces2[1].length : 0))
    }

    function accDiv(arg1, arg2) {
        let decimalPlaces1 = arg1.toString().split('.')
        let decimalPlaces2 = arg2.toString().split('.')
        return (Number(String(arg1).replace('.', '')) / Number(String(arg2).replace('.', ''))) * Math.pow(10, (decimalPlaces2.length > 1 ? decimalPlaces2[1].length : 0) - (decimalPlaces1.length > 1 ? decimalPlaces1[1].length : 0))
    }


    const result = optimizeMul([0.0299, 100])

    console.log(result, 'result');

    const result2 = optimizeDiv([result, 100])
    console.log('result2', result2);

    console.log(BigInt(2.99) / BigInt(100));

</script>

<style>
</style>

</html>